package com.vcit.system.util;


import com.vcit.system.domain.vo.ClazzResultVO;
import org.apache.poi.xwpf.usermodel.*;

import java.io.ByteArrayOutputStream;
import java.math.BigInteger;
import java.util.Date;

public class ReportCardWordUtil {


    public static byte[] generate(ClazzResultVO vo) {
        try (XWPFDocument doc = new XWPFDocument();
             ByteArrayOutputStream baos = new ByteArrayOutputStream()) {

            // 1. 编号
           /* XWPFParagraph pNo = doc.createParagraph();
            pNo.setAlignment(ParagraphAlignment.RIGHT);
            XWPFRun rNo = pNo.createRun();
            rNo.setText("编号：");
            rNo.setFontSize(10);*/

            // 2. 标题
            XWPFParagraph pTitle = doc.createParagraph();
            pTitle.setAlignment(ParagraphAlignment.CENTER);
            XWPFRun rTitle = pTitle.createRun();
            int year = new Date().getYear();
            rTitle.setText("山西信息职业技术学院"+year+"级新生报到卡");
            rTitle.setFontSize(16);
            rTitle.setBold(true);

            // 3. “同学：”行
            XWPFParagraph pName = doc.createParagraph();
            pName.setAlignment(ParagraphAlignment.LEFT);
            XWPFRun rName = pName.createRun();
            rName.setText("        " + (vo.getStudentName() != null ? vo.getStudentName() : "________") + "同学：");
            rName.setFontSize(12);

            // 4. 欢迎语和专业班级信息
            XWPFParagraph pInfo = doc.createParagraph();
            pInfo.setAlignment(ParagraphAlignment.LEFT);
            XWPFRun rInfo = pInfo.createRun();
            rInfo.setFontSize(12);
            rInfo.setText("        欢迎你到山西信息职业技术学院！根据你拟报的");
            rInfo.setText((vo.getMajorName() != null ? vo.getMajorName() : "________") + "专业，");
            rInfo.setText("你已被编入我院2025级");
            rInfo.setText((vo.getClazzName() != null ? vo.getClazzName() : "________") + "班，");
            rInfo.setText("请到以下部门报到办理有关入学手续。");

            // 5. 表格
            XWPFTable table = doc.createTable(4, 2);

            // 设置表格宽度
            int tableWidth = 8000;
            table.setWidth(String.valueOf(tableWidth));

            // 设置列宽
            int[] colWidths = {3000, 5000};
            for (int row = 0; row < 4; row++) {
                for (int col = 0; col < 2; col++) {
                    table.getRow(row).getCell(col).getCTTc().addNewTcPr().addNewTcW().setW(BigInteger.valueOf(colWidths[col]));
                }
            }
            // 设置每一行的高度为1.5厘米
            int rowHeight = 2000; // 1.5cm ≈ 850 twips
            for (int i = 0; i < table.getNumberOfRows(); i++) {
                table.getRow(i).setHeight(rowHeight);
            }
            // 设置表格内容和对齐
            String[] left = {"招生办", "财务处", "公寓用品", "生活用品"};
            for (int i = 0; i < 4; i++) {
                XWPFTableCell cellLeft = table.getRow(i).getCell(0);
                cellLeft.removeParagraph(0);
                XWPFParagraph paraLeft = cellLeft.addParagraph();
                paraLeft.setAlignment(ParagraphAlignment.CENTER); // 水平居中
                cellLeft.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); // 垂直居中
                XWPFRun runLeft = paraLeft.createRun();
                runLeft.setText(left[i]);
                runLeft.setFontSize(12);

                XWPFTableCell cellRight = table.getRow(i).getCell(1);
                cellRight.removeParagraph(0);
                XWPFParagraph paraRight = cellRight.addParagraph();
                paraRight.setAlignment(ParagraphAlignment.RIGHT); // 水平靠右
                cellRight.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.BOTTOM); // 垂直靠下

                XWPFRun runRight = paraRight.createRun();
                // 在“签字（盖章）”前加5个全角空格
                runRight.setText("　　　签字（盖章）______  "); // 5个全角空格
                runRight.setFontSize(12);
            }

            // 6. 辅导员签字栏
            XWPFParagraph pFooter = doc.createParagraph();
            pFooter.setAlignment(ParagraphAlignment.RIGHT);
            XWPFRun rFooter = pFooter.createRun();
            rFooter.addBreak();
            rFooter.setText("辅导员接收签字：____________");
            rFooter.setFontSize(12);

            doc.write(baos);
            return baos.toByteArray();
        } catch (Exception e) {
            throw new RuntimeException("生成报到卡Word失败", e);
        }
    }
}
